package com.etc.zzsys.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.zzsys.entity.User;
import com.etc.zzsys.service.UserService;
import com.etc.zzsys.service.impl.UserServiceImpl;
import com.etc.zzsys.util.MD5Util;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		UserService us=new UserServiceImpl();
		
		HttpSession session=request.getSession();
		
		String op="";
		
		if(request.getParameter("op")!=null) {
			op=request.getParameter("op");
		}
		
		if("check".equals(op)) {
			doCheck(request, response);
		}
		if("logout".equals(op)) {
			doLogout(request, response);
		}
		if("regist".equals(op)) {
			doRegist(request, response);
		}
		if(op.equals("")) {
			String username=request.getParameter("username");
			String pwd=MD5Util.getEncodeByMd5(request.getParameter("pwd"));
			User user=us.checkLogin(username, pwd);
			if(user !=null) {
				session.setAttribute("user",user);
//				Cookie cookie=new Cookie("username", user.getUserName());
//				Cookie cookie2=new Cookie("pwd", user.getUserPwd());
//				response.addCookie(cookie);
//				response.addCookie(cookie2);
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}else {
				
				PrintWriter out=response.getWriter();
				out.println("<script>alert('用户名或密码错误！');</script>");
				out.close();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void doCheck(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		UserService us=new UserServiceImpl();
		
		String data="用户名错误";
		String username=request.getParameter("username");
		if(us.getUserByName(username)!=null) {
			data="用户名正确";
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		
		PrintWriter out =response.getWriter();
		out.println(data);
		out.close();
	}
	protected void doLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		session.removeAttribute("user");

		request.getRequestDispatcher("login.html").forward(request, response);
	}
	protected void doRegist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserService us=new UserServiceImpl();
		String userName=request.getParameter("userName");
		String userPwd=MD5Util.getEncodeByMd5(request.getParameter("userPwd"));
		String userTel=request.getParameter("userTel");
		String userSex=request.getParameter("userSex");
		String userAddress=request.getParameter("userAddress");
		
		User u=new User(userName, userPwd, userSex, userTel, userAddress);
		boolean result=us.regist(u);
		PrintWriter out=response.getWriter();
		
		
		
		if(result) {
			out.print("true");
		}
		else{
			out.print("false");
		}
		out.close();
	}
}
